
//
//  DDVisitorLoginView.swift
//  SinaMicroBlog
//
//  Created by dqw on 15/7/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

// MARK: - 注册登陆协议
protocol DDVistorLoginViewDelegate: NSObjectProtocol{
    func visitorLoginViewWillLogin()
    func visitorLoginViewWillRegister()
}

class DDVisitorLoginView: UIView {

    weak var delegate:DDVistorLoginViewDelegate?
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
        loadCustomSubviews()
    
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        loadCustomSubviews()
    }
    func configView(isHome:Bool, message:String, iconName:String){
        messageLab.text = message
        icon.image = UIImage(named: iconName)
        
        isHome ? setAnimationForIcon() : sendSubviewToBack(maskIconView)
    }
    
    private func setAnimationForIcon(){
        
        let ani = CABasicAnimation(keyPath:"transform.rotation")
        ani.toValue = 2 * M_PI
        ani.repeatCount = MAXFLOAT
        ani.duration = 3.0
        ani.removedOnCompletion = false
        icon.layer.addAnimation(ani, forKey: nil)
    }
    
    private func loadCustomSubviews()
    {
        addSubview(icon)
        addSubview(maskIconView)
        addSubview(homeImg)
        addSubview(messageLab)
        addSubview(loginBtn)
        addSubview(registerBtn)
    }
    
    lazy private var icon:UIImageView = {
        
        let iconTmp = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iconTmp
        
    }()
    lazy private var homeImg:UIImageView = {
        let homeImgTmp = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
        return homeImgTmp
        
        }()
    lazy private var messageLab:UILabel = {
        let messageLabTmp = UILabel()
        messageLabTmp.font = UIFont.systemFontOfSize(14)
        messageLabTmp.textColor = UIColor.darkGrayColor()
        messageLabTmp.textAlignment = NSTextAlignment.Center
        messageLabTmp.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        messageLabTmp.sizeToFit()
        messageLabTmp.numberOfLines = 0
        return messageLabTmp
        
        }()
    lazy private var loginBtn:UIButton = {
        let loginBtnTmp = UIButton()
        loginBtnTmp.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtnTmp.setTitle("登陆", forState: UIControlState.Normal)
        loginBtnTmp.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        loginBtnTmp.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return loginBtnTmp
        }()
    lazy private var registerBtn:UIButton = {
        let registerBtnTmp = UIButton()
        registerBtnTmp.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerBtnTmp.setTitle("注册", forState: UIControlState.Normal)
        registerBtnTmp.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtnTmp.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return registerBtnTmp
        }()
    lazy private var maskIconView:UIImageView = {
        let maskViewTmp = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return maskViewTmp
        }()
    
     func loginBtnClick(){
        print("login")
        delegate?.visitorLoginViewWillLogin()
    }
    func registerBtnClick(){
        print("login")

        delegate?.visitorLoginViewWillRegister()
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
//        messageLab.center = center
        icon.translatesAutoresizingMaskIntoConstraints = false
       addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
       addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        
        homeImg.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeImg, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImg, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        messageLab.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))

        
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLab, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskView" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-200-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskView" : maskIconView]))
     

    }

    
    

}
